package cn.com.shopec.erp.purchase.commo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import cn.com.shopec.erp.common.constant.ProductSCMConstant;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.RandomUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.vo.ProductVo1;
import cn.com.shopec.erp.purchase.entity.PurchaseOrder;
import cn.com.shopec.erp.purchase.entity.PurchaseRequisitionProduct;
//import cn.com.shopec.erp.purchase.entity.PurchaseRequisition;
//import cn.com.shopec.erp.purchase.entity.PurchaseRequisitionProduct;
import cn.com.shopec.erp.purchase.vo.PurchaseOrderVo;
//import cn.com.shopec.erp.purchase.vo.PurchaseRequisitionProductVo;
//import cn.com.shopec.erp.purchase.vo.PurchaseRequisitionVo;
import cn.com.shopec.erp.purchase.vo.PurchaseRequisitionProductVo;
import cn.com.shopec.erp.system.common.UserUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: PRPropertyUtils
 * @Description: 请购单虚拟表与实体表转换
 * @author: gaoyaxiong
 * @date: 2013-5-21 下午18:14:13
 */
@Component
public class PRPropertyUtils {

	/**
	 * @methodName：getPr
	 * @Description: 请购虚拟对象转实体表对象
	 * @param PurchaseRequisitionVo
	 * @return PurchaseRequisition
	 * @author: gaoyaxiong
	 * @date: 2013-5-21 下午18:22:03
	 */
	/*public static PurchaseRequisition getPr(PurchaseRequisitionVo prv) {
		PurchaseRequisition pr = new PurchaseRequisition();
		if (LogicUtil.isNotNull(prv)) {
			pr.setId(prv.getId());
			pr.setPrNo(prv.getPrNo());
			pr.setPrName(prv.getPrName());
			pr.setPrUser(prv.getPrUser());
			pr.setPrQC(prv.getPrQC());
			pr.setPrEstimatedDate(prv.getPrEstimatedDate());
			pr.setPrDescribe(prv.getPrDescribe());
			pr.setPrCreaterDate(prv.getPrCreaterDate());
			pr.setPrStatus(prv.getPrStatus());
			pr.setPrAuditor(prv.getPrAuditor());
			pr.setPrReason(prv.getPrReason());
			pr.setPrAuditDate(prv.getPrAuditDate());
			pr.setPrAuditStatus(prv.getPrAuditStatus());
			pr.setDeleteFlag(prv.getDeleteFlag());
			pr.setPrArriveDate(prv.getPrArriveDate());
			pr.setPrType(prv.getPrType());
		}
		return pr;
	}*/
	
	//有用到

	/**
	 * @methodName：getPr
	 * @Description: 请购虚拟对象转实体表对象
	 * @param PurchaseRequisitionVo
	 * @return PurchaseRequisition
	 * @author: gaoyaxiong
	 * @date: 2013-5-21 下午18:22:03
	 */
	public static PurchaseOrder getNewRequisition(PurchaseOrderVo prv) {
		PurchaseOrder pr = new PurchaseOrder();
		Date now = new Date();

		if (LogicUtil.isNotNull(prv)) {
//			String no = "BR"+ DateUtil.formatDateByFormat(new Date(),DateUtil.NOCHAR_PATTERN)+ RandomUtil.getRandomNumberStr(5);
			String no = "PO"+ DateUtil.formatDateByFormat(new Date(),DateUtil.NOCHAR_PATTERN)+ RandomUtil.getRandomNumberStr(5);
			pr.setPoNo(no);
			if (StringUtil.isExist(prv.getPoName())) {
				pr.setPoName(prv.getPoName());
			} else {
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
				String date = dateFormat.format(now);
				pr.setPoName(date);
			}
			pr.setCreatePerson(UserUtils.getLoginUserName());//创建人
			pr.setPoStatus(ProductSCMConstant.PO_STATUS_ONE);
			pr.setCreatePerson(UserUtils.getLoginUserName());
			pr.setFoPurDate(prv.getFoPurDate());//预计采购日期
			pr.setPrDescribe(prv.getPrDescribe());//描述
			pr.setCurAuditPoint(SZSCMConstant.CUR_AUDIT_POINT);//当前审核人
			pr.setSupName(prv.getSupName());
			pr.setSupCode(prv.getSupCode());
			pr.setCreateTime(DateUtil.formatTimesTampDate(new Date()));//创建时间
			pr.setPrDescribe(prv.getPrDescribe());
			
			pr.setPrCreaterDate(DateUtil.formatTimesTampDate(new Date()));
			pr.setDeleteFlag(Integer.valueOf(SZSCMConstant.DELETE_FLAG_NO));
			pr.setPrArriveDate(prv.getPrArriveDate());
			if(StringUtil.isExist(String.valueOf(prv.getPurcType()))){
				pr.setPurcType(prv.getPurcType());
			}
			pr.setTotalPrice(prv.getTotalPrice());
		}
		return pr;
	}

	/**
	 * @methodName：getPrVo
	 * @Description: 请购实体表对象转虚拟对象
	 * @param PurchaseRequisition
	 * @return PurchaseRequisitionVo
	 * @author: gaoyaxiong
	 * @date: 2013-5-21 下午18:22:03
	 */
	/*public static PurchaseRequisitionVo getPrVo(PurchaseRequisition pr) {
		PurchaseRequisitionVo prv = new PurchaseRequisitionVo();
		prv.setId(pr.getId());
		prv.setPrNo(pr.getPrNo());
		prv.setPrName(pr.getPrName());
		prv.setPrUser(pr.getPrUser());
		prv.setPrQC(pr.getPrQC());
		prv.setPrEstimatedDate(pr.getPrEstimatedDate());
		prv.setPrDescribe(pr.getPrDescribe());
		prv.setPrCreaterDate(pr.getPrCreaterDate());
		prv.setPrStatus(pr.getPrStatus());
		prv.setPrAuditor(pr.getPrAuditor());
		prv.setPrReason(pr.getPrReason());
		prv.setPrAuditDate(pr.getPrAuditDate());
		prv.setPrAuditStatus(pr.getPrAuditStatus());
		prv.setDeleteFlag(pr.getDeleteFlag());
		prv.setPrType(pr.getPrType());
		prv.setPrArriveDate(pr.getPrArriveDate());
		return prv;
	}*/

	/**
	 * @methodName：getPrPVo
	 * @Description: 请购商品实体表对象转虚拟对象
	 * @param PurchaseRequisitionProduct
	 * @return PurchaseRequisitionProductVo
	 * @author: gaoyaxiong
	 * @date: 2013-5-21 下午18:22:03
	 */
	/*public static PurchaseRequisitionProductVo getPrPVo(
			PurchaseRequisitionProduct prp) {
		PurchaseRequisitionProductVo prpv = new PurchaseRequisitionProductVo();
		prpv.setId(prp.getId());
		prpv.setPrNo(prp.getPrNo());
		prpv.setPpName(prp.getPpName());
		prpv.setPpGoodsNo(prp.getPpGoodsNo());
		prpv.setPpSpecification(prp.getPpSpecification());
		prpv.setPpUnit(prp.getPpUnit());
		prpv.setPpNumber(prp.getPpNumber());
		prpv.setPpSupplierNo(prp.getPpSupplierNo());
		prpv.setPpSupplierName(prp.getPpSupplierName());
		prpv.setPpWarehouseNo(prp.getPpWarehouseNo());
		prpv.setPpWarehouseName(prp.getPpWarehouseName());
		prpv.setPpUnitPrice(prp.getPpUnitPrice());
		prpv.setDeleteFlag(prp.getDeleteFlag());
		prpv.setTotalPrice(prp.getTotalPrice());
		prpv.setSalePrice(prp.getSalePrice());
		return prpv;
	}*/

	/**
	 * @methodName：getPrP
	 * @Description: 请购商品实体表对象转虚拟对象
	 * @param PurchaseRequisitionProductVo
	 * @return PurchaseRequisitionProduct
	 * @author: gaoyaxiong
	 * @date: 2013-5-21 下午18:22:03
	 */
	/*public static PurchaseRequisitionProduct getPrP(
			PurchaseRequisitionProductVo prpv) {

		PurchaseRequisitionProduct prp = new PurchaseRequisitionProduct();
		prp.setId(prpv.getId());
		prp.setPrNo(prpv.getPrNo());
		prp.setPpName(prpv.getPpName());
		prp.setPpGoodsNo(prpv.getPpGoodsNo());
		prp.setPpSpecification(prpv.getPpSpecification());
		prp.setPpUnit(prpv.getPpUnit());
		prp.setPpNumber(prpv.getPpNumber());
		prp.setPpSupplierNo(prpv.getPpSupplierNo());
		prp.setPpSupplierName(prpv.getPpSupplierName());
		prp.setPpWarehouseNo(prpv.getPpWarehouseNo());
		prp.setPpWarehouseName(prpv.getPpWarehouseName());
		prp.setPpUnitPrice(prpv.getPpUnitPrice());
		prp.setDeleteFlag(prpv.getDeleteFlag());
		prp.setTotalPrice(prpv.getTotalPrice());
		prp.setSalePrice(prpv.getSalePrice());
		return prp;
	}*/
	
	/**
	 * @methodName：getPrP
	 * @Description: 请购商品实体表对象转虚拟对象
	 * @param PurchaseRequisitionProductVo
	 * @return PurchaseRequisitionProduct
	 * @author: gaoyaxiong
	 * @date: 2013-5-21 下午18:22:03
	 */
	/*public static void updateToprp(
			PurchaseRequisitionProduct prp,PurchaseRequisitionProductVo prpv) {
		
		prp.setPpNumber(prpv.getPpNumber());
		prp.setPpSupplierNo(prpv.getPpSupplierNo());
		prp.setPpSupplierName(prpv.getPpSupplierName());
		prp.setPpWarehouseNo(prpv.getPpWarehouseNo());
		prp.setPpWarehouseName(prpv.getPpWarehouseName());
		prp.setPpUnitPrice(prpv.getPpUnitPrice());
		prp.setTotalPrice(prpv.getTotalPrice());
		prp.setSalePrice(prpv.getSalePrice());
	}

	*//**
	 * @methodName：getPrP
	 * @Description: 添加请购商品表
	 * @param PurchaseRequisitionProductVo
	 * @return PurchaseRequisitionProduct
	 * @author: gaoyaxiong
	 * @date: 2013-5-21 下午18:22:03
	 */
	public static PurchaseRequisitionProduct getNewPrP(
			PurchaseRequisitionProductVo prpv, ProductVo1 productVo) {

		PurchaseRequisitionProduct prp = new PurchaseRequisitionProduct();
//		prp.setPrNo(prpv.getPrNo());
		prp.setPoNo(prpv.getPoNo());
		prp.setPpName(productVo.getCommoName());
		prp.setProductNo(productVo.getProdNo());
//		prp.setPpSpecification(productVo.getCasePakgSize());//箱包装规格
		prp.setPpSpecification(productVo.getOptionNames());
		prp.setPpNumber(prpv.getPpNumber());
		prp.setBuyNum(prpv.getBuyNum());
		prp.setPpSupplierNo(prpv.getPpSupplierNo());
		prp.setPpSupplierName(prpv.getPpSupplierName());
		prp.setPpWarehouseNo(prpv.getPpWarehouseNo());
		prp.setPpWarehouseName(prpv.getPpWarehouseName());
		prp.setPpUnitPrice(prpv.getPpUnitPrice());
		prp.setTotalPrice(prpv.getTotalPrice());
		prp.setSalePrice(prpv.getSalePrice());
		prp.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
		prp.setCommodityNo(prpv.getCommodityNo());
		prp.setProdBarCode(prpv.getProdBarCode());
		prp.setManufactureTime(prpv.getManufactureTime());
		prp.setShelfLife(prpv.getShelfLife());
		return prp;
	}

	/**
	 * @methodName：addRequisition
	 * @Description: 请购商品json转请购商品对象集合
	 * @param
	 * @return String
	 * @author: gaoyaxiong
	 * @throws Exception
	 * @date: 2013-5-23 下午13:10:03
	 */
	public List<PurchaseRequisitionProductVo> getStringToProduct(String data,
			boolean isSave) {
		List<PurchaseRequisitionProductVo> productlist = new ArrayList<PurchaseRequisitionProductVo>();
		if (data == null || data.equals("")) {
			return productlist;
		}
		JSONArray jsonArray = null;
		try {
			jsonArray = JSONArray.fromObject(data);
		} catch (Exception e) {
			return productlist;
		}

		if (jsonArray != null && jsonArray.size() > 0) {
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonJ = jsonArray.getJSONObject(i);
				if (jsonJ != null) {								
					PurchaseRequisitionProductVo product = new PurchaseRequisitionProductVo();
					product.setProductNo(getString(jsonJ,"prodNo"));
					if (isSave) {
						product.setPpName(getString(jsonJ,"commoName"));
						product.setPpSpecification(getString(jsonJ,"casePakgSize"));
					}
//					product.setPoNo(jsonJ.getString("poNo"));
					product.setId(getString(jsonJ,"id"));
					product.setPpName(getString(jsonJ,"commoName"));
					if(getString(jsonJ,"number")==null){
						product.setPpNumber(getString(jsonJ,"number")==null?0:Integer.valueOf(getString(jsonJ,"number")));
					}
					if(getString(jsonJ,"ppNumber")!=null){
						product.setPpNumber(getString(jsonJ,"ppNumber")==null?0:Integer.valueOf(getString(jsonJ,"ppNumber")));
					}
					if(getString(jsonJ,"buyNum")!=null){
						product.setBuyNum(getString(jsonJ,"buyNum")==null?0:Integer.valueOf(getString(jsonJ,"buyNum")));
					}
					/*product.setPpSupplierNo(jsonJ.getString("purSuppNo"));
					product.setPpSupplierName(jsonJ.getString("purSuppName"));*/
					product.setPpWarehouseNo(getString(jsonJ,"purWhNo"));
					product.setPpWarehouseName(getString(jsonJ,"purWhName"));
					/*product.setPpUnitPrice(jsonJ.getString("actprice"));*/
					product.setTotalPrice(getString(jsonJ,"totalPrice"));
					product.setSalePrice(getString(jsonJ,"salePrice"));
					product.setProdBarCode(getString(jsonJ,"prodBarCode"));
					product.setCommodityNo(getString(jsonJ,"commoNo"));
					product.setPpUnitPrice(getString(jsonJ,"ppUnitPrice"));
					product.setShelfLife(getString(jsonJ, "shelfLife"));
					product.setManufactureTime(getString(jsonJ, "manufactureTime").substring(0, 10));
					try {
						product.setManufactureTime(DateUtil.getDateNextDay(product.getManufactureTime()));
					} catch (ParseException e) {
						e.printStackTrace();
					}
					productlist.add(product);
				}
			}
		}
		return productlist;
	}
	
	public String getString(JSONObject json,String key){
		if(json.containsKey(key)){
			return json.getString(key);
	    }
		return null;
	}
	
}
